package com.nian.zipkin.server;

import org.slf4j.bridge.SLF4JBridgeHandler;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import zipkin2.server.internal.EnableZipkinServer;
import zipkin2.server.internal.ZipkinActuatorImporter;
import zipkin2.server.internal.ZipkinModuleImporter;
import zipkin2.server.internal.banner.ZipkinBanner;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/3/30 0030 13:23
 */
@EnableZipkinServer
@SpringBootApplication
public class NianZipKinServerApplication {
    public static String[] baseParam = new String[]{
            EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY + "=false",
            "spring.config.name=zipkin-server"
    };
    public static void main(String[] args) {
        SpringApplicationBuilder application = new SpringApplicationBuilder(new Class[] { NianZipKinServerApplication.class });
        application.banner(new ZipkinBanner()).initializers(new ZipkinModuleImporter(), new ZipkinActuatorImporter());
        application.properties(baseParam);
        application.run(args);
        // 打印配置参数：
//        readEnvironmentParam(application);

    }

    /**
     * 打印配置文件参数
     * @param application
     */
    public static void readEnvironmentParam(SpringApplicationBuilder application) {
        // 配置参数：
        ConfigurableEnvironment environment = application.context().getEnvironment();
        System.out.println("******************************* 服务环境 ************************************");
        MutablePropertySources pro = environment.getPropertySources();
        pro.forEach(propertySource -> {
            System.out.println(propertySource.getName()+" -:- " + propertySource.getSource().getClass());
        });
        System.out.println("************************* zipkin-server-shared.yml 参数 *********************");
        MapPropertySource defaultPro = (MapPropertySource) pro.get("applicationConfig: [classpath:/zipkin-server-shared.yml]");
        String[] proArray = defaultPro.getPropertyNames();
        for (String d: proArray) {
            System.out.println(d + ":" + environment.getProperty(d));
        }
        System.out.println("******************************* 欢迎欢迎 ************************************");
    }

    static {
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();
        System.setProperty("MYSQL_HOST", "192.168.203.1");
        System.setProperty("STORAGE_TYPE", "mysql");
        System.setProperty("MYSQL_HOST", "192.168.203.1");
        System.setProperty("MYSQL_TCP_PORT", "3306");
        System.setProperty("MYSQL_DB", "nian_monitor");
        System.setProperty("MYSQL_USER", "nian");
        System.setProperty("MYSQL_PASS", "nian");

        System.setProperty("SCRIBE_ENABLED", "true");
        System.setProperty("SELF_TRACING_ENABLED", "true");
        System.setProperty("RABBIT_ADDRESSES", "192.168.203.132:5672");
        System.setProperty("RABBIT_USER", "guest");
        System.setProperty("RABBIT_PASSWORD", "guest");
        System.setProperty("RABBIT_QUEUE", "zipkin");
        System.setProperty("RABBIT_VIRTUAL_HOST", "/admin_host");
        System.setProperty("JAVA_OPTS", "-Dlogging.level.zipkin2=DEBUG");
    }

//        System.out.println("spring.application.name: " + environment.getProperty("spring.application.name"));
//        System.out.println("zipkin.storage.type: " + environment.getProperty("zipkin.storage.type"));
//        System.out.println("zipkin.storage.mysql.host: " + environment.getProperty("zipkin.storage.mysql.host"));
//        System.out.println("zipkin.storage.mysql.port: " + environment.getProperty("zipkin.storage.mysql.port"));
//        System.out.println("zipkin.storage.mysql.db: " + environment.getProperty("zipkin.storage.mysql.db"));
//        System.out.println("zipkin.storage.mysql.jdbc-url: " + environment.getProperty("zipkin.storage.mysql.jdbc-url"));
//        System.out.println("zipkin.storage.mysql.username: " + environment.getProperty("zipkin.storage.mysql.username"));
//        System.out.println("zipkin.storage.mysql.password: " + environment.getProperty("zipkin.storage.mysql.password"));
}